 package com.jeecg;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

import javax.print.attribute.standard.PrinterStateReason;

public class ServerSocketHandler extends Thread{
	private Socket socket;
	private InputStream in;
	private PrintStream out;
	public static final String 	WebRoot="D:\\JavaDevelopEnvironment\\JavaCode\\WebServer\\htdocs";
	
	/**
	 * 从socket建立输入流，输出流
	 * @param socket
	 */
	public ServerSocketHandler(Socket socket){
		this.socket = socket;
		try {
			in=socket.getInputStream();
			out=new PrintStream(socket.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 在线程中进行socket通信(IO流)
	 */
	public void run(){
		String filename = getFileName(in);
		if(filename!=null){
			doResponse(filename);
		}
	}
	
	/**
	 * 解析socket输入流，返回请求的文件名称
	 * @param in
	 * @return
	 */
	public String getFileName(InputStream in){
		String filename=null;
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		
		try {
			String HttpMessage=br.readLine();
			if(HttpMessage == null){
				sendErrorMessage(400, "Client query error!");
				return null;
			}
			
			String[] content=HttpMessage.split(" ");
			if(content!=null && content.length != 3){
				sendErrorMessage(400, "Client query error!");
				return null;
			}
			
			System.out.println("protocol:" + content[0] + ",  filename:" + content[1] + ",  http version:" + content[2]);
			filename=content[1];
			return filename;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 根据文件名，返回socket输出流
	 * @param filename
	 */
	public void doResponse(String filename){
		File file = new File(ServerSocketHandler.WebRoot + filename);
		if(!file.exists()){
			sendErrorMessage(404, "File not found!");
			return;
		}
		
		try {
			InputStream in=new FileInputStream(file);
			byte content[]=new byte[(int)file.length()];
			in.read(content);
			out.println("HTTP/1.0 200 OK");
			out.println("MIME_Version:1.0");
			out.println("Content-Type:text/html; charset=utf-8");
			out.println("content-length:"+content.length);
			
			out.println();
			out.write(content);
			out.flush();
			out.close();
			in.close();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 使用输出流，打印错误提示信息
	 * @param errorcode
	 * @param errorMessage
	 */
	public void sendErrorMessage(int errorcode, String errorMessage){
		out.println("HTTP/1.0" + errorcode + " " + errorMessage);
		out.println("MIME_Version:1.0");
		out.println("Content-Type:text/html; charset=utf-8");
		out.println();
		out.println("<html>");
		out.println("<title>Error Message");
		out.println("</title>");
		out.println("<body>");
		out.println("<h1>ErrorCode:"+errorcode+",  Message:"+errorMessage+"</h1>");
		out.println("</body>");
		out.println("</html>");
		out.flush();
		out.close();
		try {
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
